package com.bjpowernode.boss.mvc.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.convert.PaymentConvert;
import com.bjpowernode.boss.enums.RespInfo;
import com.bjpowernode.boss.exception.PowerException;
import com.bjpowernode.boss.mvc.models.dic.DicItem;
import com.bjpowernode.boss.mvc.models.query.PayProductConfigQuery;
import com.bjpowernode.boss.mvc.models.response.AjaxResult;
import com.bjpowernode.boss.mvc.models.vo.PayProductVo;
import com.bjpowernode.boss.mvc.models.vo.PayWayVo;
import com.bjpowernode.boss.mvc.service.PaymentService;
import com.bjpowernode.boss.persist.domain.PayProduct;
import com.bjpowernode.boss.persist.domain.PayWay;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private PaymentConvert paymentConvert;

    @GetMapping("/pay/product/loadPage/{pageNo}")
    public AjaxResult loadPage(@PathVariable Integer pageNo) {

        Page<PayProduct> payProductPage = paymentService.loadPage(pageNo);

        List<PayProductVo> records = paymentConvert.payProductListToVoList(payProductPage.getRecords());
        long pageCount = payProductPage.getPages();
        long size = payProductPage.getSize();

        Map<String, Object> map = new HashMap<>();
        map.put("payProductList", records);
        map.put("pages", pageCount);
        map.put("size", size);

        return AjaxResult.OK(map);
    }

    @GetMapping("/pay/product")
    public AjaxResult loadProduct() {
        List<DicItem> productList = paymentService.getAllProduct();
        return AjaxResult.OK(productList);
    }

    @GetMapping("/pay/way")
    public AjaxResult loadWay() {
        List<DicItem> wayList = paymentService.getAllWay();
        return AjaxResult.OK(wayList);
    }

    @GetMapping("/pay/type/{code}")
    public AjaxResult loadType(@PathVariable("code") String payWayCode) {
        if (StrUtil.isNotBlank(payWayCode)) {
            List<DicItem> payTypeList = paymentService.getPayTypeList(payWayCode.toUpperCase());
            return AjaxResult.OK(payTypeList);
        }
        return AjaxResult.FAIL(RespInfo.REQUEST_PARAM_ERROR);
    }

    @PostMapping("/pay/product/config")
    public AjaxResult addPayWay(@RequestBody PayProductConfigQuery payProductConfigQuery) {

        paymentService.addPayWay(payProductConfigQuery);
        return AjaxResult.OK();
    }

    @GetMapping({"/pay/way/list/{pageNo}/{productCode}", "/pay/way/list/{pageNo}"})
    public AjaxResult loadWayList(@PathVariable Integer pageNo, @PathVariable(required = false) String productCode) {

        Page<PayWay> payWayPage = paymentService.loadPayWayList(pageNo, productCode);

        List<PayWay> records = payWayPage.getRecords();
        List<PayWayVo> payWayVoList = paymentConvert.payWayListToVoList(records);
        long total = payWayPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("payWayList", payWayVoList);
        map.put("total", total);

        return AjaxResult.OK(map);
    }

    //删除支付方式
    @DeleteMapping("/pay/way/remove/{id}")
    public AjaxResult removePayWay(@PathVariable String id) {

        paymentService.removePayWay(id);
        return AjaxResult.OK();

    }

    //产品名称模糊查询产品
    @GetMapping("/pay/product/matchName/{productName}")
    public AjaxResult matchName(@PathVariable String productName) {

        if (StrUtil.isNotBlank(productName) && productName.trim().length() >= 2) {
            List<PayProduct> productList = paymentService.matchProductName(productName);
            return AjaxResult.OK(productList);
        }
        throw new PowerException(RespInfo.REQUEST_PARAM_ERROR);
    }

}
